//
//  ViewController.m
//  TextfieldPoint
//
//  Created by lcy on 15/12/18.
//  Copyright © 2015年 lcy. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    self.view.backgroundColor = [UIColor blackColor];
    
    // UITextField创建以及常用属性
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 100, 30)];

//    textField.layer.cornerRadius = 5;
//    textField.layer.borderWidth = 1;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.text = @"史上";
    textField.textAlignment = NSTextAlignmentCenter;
    textField.clearsOnBeginEditing = YES;
    textField.delegate = self;
//    textField.placeholder = @"史上最帅的程序员";
//    textField.backgroundColor = [UIColor redColor];
    
    textField.returnKeyType = UIReturnKeyDone;
    
    //    textField.borderStyle = UITextBorderStyleRoundedRect;
    //    secondTextField.borderStyle = UITextBorderStyleRoundedRect;
    
    [self.view addSubview:textField];
    

//    UITextField *secondTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, 100, 50)];
//
//    secondTextField.backgroundColor = [UIColor yellowColor];
//    [self.view addSubview:secondTextField];
    
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 200, 30)];
    [self.view addSubview:self.label];

}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//    [textField resignFirstResponder];
    [textField endEditing:YES];
    
//    self.label.text = textField.text;
    
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    NSLog(@"%@ %@ %lu", NSStringFromRange(range), string, range.location - range.length);
    if(range.location - range.length >=10)
    {
        self.label.text = @"字数超出限制";
        self.label.textColor = [UIColor redColor];
    }else
    {
        self.label.text = @"未超出限制";
        self.label.textColor = [UIColor greenColor];
    }
    
    
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    NSLog(@"dd");
    return YES;
}

/*
 
 
 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        // return NO to disallow editing.
 - (void)textFieldDidBeginEditing:(UITextField *)textField;           // became first responder
 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField;          // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
 - (void)textFieldDidEndEditing:(UITextField *)textField;             // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
 
 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text
 
 - (BOOL)textFieldShouldClear:(UITextField *)textField;               // called when clear button pressed. return NO to ignore (no notifications)
 - (BOOL)textFieldShouldReturn:(UITextField *)textField;              // called when 'return' key pressed. return NO to ignore.
 

 
 
 */

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
